<?php
namespace app\admin\controller;
use app\admin\model\Notice as noticeModel;
use think\Db;
class Notice extends Common
{
    function index(){
        $noticeModel = new noticeModel();
        $noticeData = $noticeModel->noticeData();
        $getData = input('get.');

        $this->assign('list',$noticeData['list']);
        $this->assign('page',$noticeData['page']);
        $this->assign('count',$noticeData['count']);
        $this->assign('getData',$getData);

        return $this->fetch();
    }

    // function ajaxNoticeList(){
    //     $noticeModel = new noticeModel();
    //     $noticeData = $noticeModel->noticeData();

    //     $this->assign('list',$noticeData['list']);
    //     $this->assign('page',$noticeData['page']);
    //     $this->assign('count',$noticeData['count']);

    //     return $this->fetch('ajaxNoticeList');
    // }

    function noticeEdit(){
        if(request()->isAjax()){
//            (new \app\admin\validate\Notice()) -> goCheck();
            $postData = input('post.');
//            dump($postData);exit;
            $data['title'] = $postData['title'];
            $data['author'] = $postData['author'];
            $data['content'] = $postData['content'];
            $data['sort_order'] = $postData['sort_order']>0 ? $postData['sort_order'] : 999;
            $data['add_time'] = strtotime($postData['add_time']);
            if($postData['noticeid'] > 0){
                $res = Db::name('notice')->where("noticeid",$postData['noticeid'])->update($data);
                if($res!==false){
                    exitjson(1,'保存成功');
                }else{
                    exitjson(0,'保存失败');
                }
            }else{
                $res = Db::name('notice')->insertGetId($data);
                if($res){
                    exitjson(1,'保存成功');
                }else{
                    exitjson(0,'保存失败');
                }
            }

        }else{
            $noticeid = input('id');
            $date = date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']);
            if($noticeid>0){
                $one = Db::name('notice')->find($noticeid);
                $date = date('Y-m-d H:i:s',$one['add_time']);
                $this->assign('one',$one);
            }
            $this->assign('date',$date);
            return $this->fetch('noticeEdit');
        }

    }

    function noticeHide(){
        $id = input('id');
        $data['is_del'] = 1;
        $res = Db::name('notice')->where("noticeid",$id)->update($data);
        if($res){
            exitjson(1,'删除成功');
        }else{
            exitjson(0,'删除失败');
        }
    }

}